﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public static class MyUtil
{

    public static T Clamp<T>(T value, T min, T max) where T : System.IComparable<T>
    {
        return value.CompareTo(max) > 0 ? max : value.CompareTo(min) < 0 ? min : value;
    }

    public static void Clamp<T>(T value, T min, T max, out T result) where T : System.IComparable<T>
    {
        result = Clamp(value, min, max);
    }

    public static Transform GetChild( Transform parent, string name )
    {
        return parent.GetComponentsInChildren<Transform>(true).FirstOrDefault(d => d.name.Equals(name));
    }

    public static void UpdateLayer(Transform parent, int layer)
    {
        parent.gameObject.layer = layer;
        Transform[] Child = parent.GetComponentsInChildren<Transform>();
        foreach (Transform child in Child)
            child.gameObject.layer = layer;
    }

    public static Vector2 Vector3to2( Vector3 v ) { return new Vector2( v.x, v.y ); }
    public static Vector3 Vector2to3( Vector2 v ) { return new Vector3( v.x, v.y, 0f ); }

    public static string FindClassWithModelName(string modelName)
    {
        int _Index = modelName.IndexOf("_");
        string ModelClass = modelName;
        if (_Index != -1)
            ModelClass = modelName.Remove(_Index);

        return ModelClass;
    }


    public static string FindWeaponClassWithItemDef(string modelName)
    {
        int _Index = modelName.IndexOf("_");
        string ModelClass = modelName;
        if (_Index != -1)
            ModelClass = modelName.Remove(_Index);

        return ModelClass;
    }
}